class MarcHash < Hash
  attr_accessor :myhash, :a
  def initialize(tag, code ={})
    @a=[0]
    @myhash={tag => {1 => code }}
  end

  def getHighestPos(tag, code)
    if @myhash.keys.include?(tag)
      @myhash[tag].each {|pos, c|
        c.keys.each {|cc|
          if cc == code
            @a << pos
          end
        }
      }
      #p @a.compact
      return @a.compact.max+1
    else
      @a=[0]
      return 0
    end
  end

 
  def getPos(tag, code)
    @a=[0]
     if @myhash.keys.include?(tag)
      @myhash[tag].each {|pos, c|
        c.keys.each {|cc|
          if cc == code
            @a << pos
          end
        }
      }
        return @a.max
     end
  end






  def addHash(tag, code ={})
    if @myhash.key?(tag)
      posmax=nil
      @myhash[tag].each { |pos, c|
        if c.keys.include?(code.keys[0])
        #  p tag + String(code)
          posmax=getPos(tag, code.keys[0])+1
          #p posmax
          break
        else
          @myhash[tag][pos].update(code)
          break
        end
      }
      if posmax and @myhash[tag].key?(posmax-1)
        if @myhash[tag][posmax-1].key?(code.keys[0])
          p true
          p code
          @myhash[tag].update({posmax=>code})
        else
          p false
          @myhash[tag][posmax]=code
        end
      end
    else
      @myhash[tag]={1 => code }
    end
  end
end


